<?php 
global $_W,$_GPC;
$act = trim($_GPC['act']);
$act = !empty($act)?$act:'list';
$_GPC['do'] = 'task';

if($act == 'list'){
	$where = "";
	$params = array(':uniacid'=>$_W['uniacid']);
	if(!isset($_GPC['status'])){
		$where = " AND status >= :status ";
		$params[':status'] = 1;
	}else{
		$where = " AND status = :status ";
		$params[':status'] = intval($_GPC['status']);
	}
	if(!empty($_GPC['openid'])){
		$where .=" AND openid = :openid";
		$params[':openid'] = trim($_GPC['openid']);
	}

	$sql = "SELECT * FROM ".tablename('imeepos_runner3_tasks')." WHERE uniacid = :uniacid {$where} ORDER BY create_time DESC";
	$list = pdo_fetchall($sql,$params);

	if(!empty($list)){
		foreach ($list as &$li){
			$li['icon'] = tomedia($li['icon']);
			$li['start_time'] = date('Y-m-d',$li['start_time']);
			$li['end_time'] = date('Y-m-d',$li['end_time']);

			$sql = "SELECT * FROM ".tablename('imeepos_runner3_listenlog')." WHERE taskid = :taskid";
			$params = array(':taskid'=>$li['id']);
			$li['in_num'] = pdo_fetchcolumn($sql,$params);

			$user = M('member')->getInfo($li['openid']);

			$sql = "SELECT * FROM ".tablename('mc_mapping_fans')." WHERE uniacid = :uniacid AND openid = :openid";
			$params = array(':uniacid'=>$_W['uniacid'],':openid'=>$li['openid']);
			$fans = pdo_fetch($sql,$params);
			$user['fansid'] = $fans['fanid'];

			$li['user'] = $user;
			if($li['status'] == 0){
				$li['statustitle'] = '未支付';
				$li['status_label'] = 'label-danger';
			}else if($li['status'] == 1){
				$li['statustitle'] = '等待接单';
				$li['status_label'] = 'label-warning';
			}else if($li['status'] == 2){
				$li['statustitle'] = '已受理';
				$li['status_label'] = 'label-default';
			}else if($li['status'] == 3){
				$li['statustitle'] = '已完成';
				$li['status_label'] = 'label-primary';
			}else if($li['status'] == 4){
				$li['statustitle'] = '已结款';
				$li['status_label'] = 'label-info';
			}

			if($li['type'] == 0){
				$li['typetitle'] = '帮我送';
				$li['type_label'] = 'label-danger';
			}else if($li['type'] == 1){
				$li['typetitle'] = '预约取';
				$li['type_label'] = 'label-warning';
			}else{
				$li['typetitle'] = '帮我买';
				$li['type_label'] = 'label-info';
			}
		}
		unset($li);
	}
}

if($act == 'edit'){
	$id = intval($_GPC['id']);
	$item = M('tasks')->getInfo($id);
	include $this->template('task_edit');
	exit();
}

if($act == 'edit_item'){
	$id = intval($_GPC['id']);
	$item = M('tasks')->getInfo($id);
	$item = $item['detail'];
	$item['images'] = unserialize($item['images']);
	include $this->template('task_edit_item');
	exit();
}

$input = $_GPC['__input'];
if($act == 'search_by_keyword'){
	$keyword = $input['keyword'];
	$where = " AND isrunner = 1";
	$list = M('member')->getList(1,$where);
	if(empty($list['list'])){
		$data = array();
		$data['status'] = 0;
		$data['list'] = array();
	}else{
		$data = array();
		$data['status'] = 1;
		$data['list'] = $list['list'];
	}
	die(json_encode($data));
}

if($act == 'set_this_search'){
	//指派接单

	$taskid = intval($input['taskid']);
	$id = $taskid;
	$user_id = intval($input['user_id']);

	$user = M('member')->getById($user_id);
	$task = M('tasks')->getInfo($taskid);

	load()->model('mc');
	$runner = mc_fetch($user['openid']);

	if($task['openid'] == $user['openid']){
		$return = array();
		$return['success'] = 1;
		$return['message'] = '这是他自己的任务哦！';
		die(json_encode($return));
	}
	$task['limit_time'] = intval($task['limit_time']);
	if($task['limit_time'] < time()){
		$return = array();
		$return['success'] = 1;
		$return['message'] = '任务已过期，不能抢单！';

		$content = "";
		$content = "系统提醒，您有一个任务已过期~\n";
		$content .= "时间：".date('Y年m月d日 h点i分')."\n";
		$content .= "咚咚咚，您有一个任务已过期，请及时延时或者加价!";
		$url = $_W['siteroot'].'app/'.$this->createMobileUrl('detail',array('id'=>$task['id']));
		mc_notice_consume2($task['openid'], '系统提醒', $content, $url,'');

		die(json_encode($return));
	}
	if(empty($task) || $task['status'] != 1){
		$return = array();
		$return['success'] = 1;
		$return['message'] = '您来晚了，订单已被抢！';
		die(json_encode($return));
	}else{
		$data = array();
		$data['openid'] = $user['openid'];
		$data['uniacid'] = $_W['uniacid'];
		$data['taskid'] = $id;
		$data['create_time'] = time();

		pdo_insert('imeepos_runner3_recive',$data);
		$reciveid = pdo_insertid();
		pdo_update('imeepos_runner3_tasks',array('status'=>2),array('id'=>$id));
		//发送消息
		$content = "";
		$content = "恭喜您，您的任务已被".$runner['nickname']."接单，请注意任务动态~\n";
		$content .= "时间：".date('Y年m月d日 h点i分')."\n";
		$content .= "咚咚咚，您的任务已被受理".$task['code']."，请注意保存，不要泄露~点击详情查看详情";
		$url = $_W['siteroot'].'app/'.$this->createMobileUrl('detail',array('id'=>$task['id']));
		mc_notice_consume2($task['openid'], '您的任务已被受理提醒', $content, $url,'');

		$member = M('member')->getInfo($user['openid']);

		$member['realname'] = !empty($member['realname'])?$member['realname']:$member['nickname'];

		$content = "【".$member['realname']."】,受理此任务！联系电话".$member['mobile'];
		$data = array();
		$data['uniacid'] = $_W['uniacid'];
		$data['openid'] = $user['openid'];
		$data['create_time'] = time();
		$data['taskid'] = $task['id'];
		$data['content'] = $content;
		M('tasks_log')->update($data);

		$return = array();
		$return['success'] = 0;
		$return['message'] = '恭喜您，抢单成功！';
		$return['taskid'] = $task['id'];
		$return['id'] = $reciveid;
		die(json_encode($return));
	}
}

if($act == 'delete'){
	$id = intval($_GPC['id']);
	pdo_delete('imeepos_runner3_tasks',array('id'=>$id));
}
include $this->template('web/task/task');